package com.k2.exstrasensorytypredictor.containers;

public class UserPoints {
	private static UserPoints instance = null;
	
	private int callTimes;
	private int userAmount;
	
	private User[] users;
	
	private boolean usersAreSet;
	
	private UserPoints(int userAmount){
		this.callTimes = 0;
		this.userAmount = userAmount;
		users = new User[userAmount];
		usersAreSet = false;
	}
	
	public static UserPoints getInstance(int userAmount){
		if (instance == null){
			instance = new UserPoints(userAmount);
		}
		
		return instance;
	}
	
	public static UserPoints getInstance(){
		
		return instance;
	}
	
	public void addUser(String name){
		if (!usersAreSet) {
			for (int i = 0; i < users.length; i++) {
				users[i] = new User();
				users[i].name = name + String.valueOf(i);
			}
			usersAreSet = true;
		}
	}
	
	public void setScores(int scores){
		if (userAmount == 1){
			users[0].scores += scores;
		} else {
			if (++callTimes % 2 != 0){
				users[0].scores += scores;
			} else {
				users[1].scores += scores;
			}
		}
	}
	
	public int getUserAmount(){
		return this.users.length;
	}
	
	public User[] getUsers(){
		return this.users;
	}
	
	public void switchActiveUser() {
		if (users.length > 1) {
			if (!users[0].isActive) {
				users[1].isActive = false;
				users[0].isActive = true;
			} else {
				users[0].isActive = false;
				users[1].isActive = true;
			}
		}
	}
	public int getActiveUserIndex(){
		int index = 0;
		if (users.length > 1) {
			if (users[0].isActive)
				index = 0;
			else if (users[1].isActive)
				index = 1;
		}
		return index;
	}
	
	public void resset(){
		for (int i = 0; i < users.length; i++) {
			users[i] = null;
		}
		callTimes = 0;
		usersAreSet = false;
	}

	public boolean isUsersAreSet() {
		return usersAreSet;
	}
	
	public void setDefaultActiveUser(){
		if (users.length > 1){
			users[0].isActive = true;
			users[1].isActive = false;
		}
	}
}
